package cn.siat.gene.controller;

import cn.siat.common.core.exception.ServiceException;
import cn.siat.common.core.web.controller.BaseController;
import cn.siat.common.core.web.domain.AjaxResult;
import cn.siat.common.log.annotation.Log;
import cn.siat.common.log.enums.BusinessType;
import cn.siat.gene.domain.DesignTBProjectMembers;
import cn.siat.gene.domain.dto.AddProjectMembersDto;
import cn.siat.gene.domain.dto.RemoveProjectMemberDto;
import cn.siat.gene.service.IDesignTBProjectMembersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 项目成员Controller
 * 
 * @author siat
 * @date 2022-10-08
 */
@RestController
@RequestMapping("/project/members")
public class DesignTBProjectMembersController extends BaseController {
    @Autowired
    private IDesignTBProjectMembersService designTBProjectMembersService;

    /**
     * 查询项目成员列表
     */
    @GetMapping("/list/{projectId}")
    public AjaxResult listProjectMembers(@PathVariable String projectId)
    {
        List<Map<String, String>> list = designTBProjectMembersService.selectDesignTBProjectMembersList(projectId);
        return AjaxResult.success(list);
    }

    /**
     * 新增项目成员
     */
    @Log(title = "项目成员", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    public AjaxResult saveProjectMember(@RequestBody AddProjectMembersDto addMembers){
        designTBProjectMembersService.insertDesignTBProjectMembers(addMembers);
        return AjaxResult.success();
    }

    /**
     * 删除项目成员
     */
    @Log(title = "项目成员", businessType = BusinessType.DELETE)
	@DeleteMapping("/remove")
    public AjaxResult removeProjectMember(@RequestBody RemoveProjectMemberDto removeMember){
        designTBProjectMembersService.deleteDesignTBProjectMembers(removeMember);
        return AjaxResult.success();
    }

    /**
     * 查询添加成员列表
     */
    @GetMapping("/{projectId}")
    public AjaxResult listNonProjectMembers(@PathVariable String projectId)
    {
        List<Map<String, String>> list = designTBProjectMembersService.selectNonDesignTBProjectMembersList(projectId);
        return AjaxResult.success(list);
    }

    /**
     * 查询当前登录用户是否为指定项目成员
     */
    @GetMapping("/isMember/{projectId}")
    public AjaxResult isProjectMember(@PathVariable String projectId){
        Boolean isMember = designTBProjectMembersService.isProjectMember(projectId);
        return AjaxResult.success(isMember);
    }

    /**
     * 更改项目成员权限
     */
    @Log(title = "项目成员", businessType = BusinessType.UPDATE)
    @PutMapping("/permission")
    public AjaxResult updatePermission(@RequestBody Map<String,String> map){
        designTBProjectMembersService.updatePermission(map);
        return AjaxResult.success();
    }

    /**
     * 查询指定项目成员
     *
     */
    @GetMapping("/member")
    public AjaxResult selectMember(RemoveProjectMemberDto member) {
        Boolean flag = designTBProjectMembersService.isProjectMember(member.getProjectId());
        if(!flag){
            throw new ServiceException("没有查看该项目权限");

        }
        return AjaxResult.success(designTBProjectMembersService.selectMember(member));
    }

    /**
     * 删除所有项目中指定userId的项目成员
     */
    @PutMapping("/delete")
    public AjaxResult deleteMember(@RequestBody String[] staffIds){
        designTBProjectMembersService.deleteMember(staffIds);
        return AjaxResult.success();
    }
}
